package com.example.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.pojo.Orders;
import com.example.springboot.service.OrdersService;
import com.example.springboot.pojo.Result;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @PostMapping("/create")
    public Result create(@RequestBody Orders orders) {
        ordersService.create(orders);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        ordersService.delete(id);
        return Result.success();
    }

    @GetMapping("/findAll")
    public Result findAll(){
        return Result.success(ordersService.findAll());
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        return Result.success(ordersService.findById(id));
    }

    @PostMapping("/update")
    public Result update(@RequestBody Orders orders){
        ordersService.update(orders);
        return Result.success();
    }
    @GetMapping("/findByUserId")
    public Result findbyUserId(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize ){
        return Result.success(ordersService.findByUserId(pageNum, pageSize));
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(ordersService.selectPage(pageNum, pageSize));
    }
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ordersService.deleteBatch(ids);
        return Result.success();
    }
    @GetMapping("/findByOrdersNumber")
    public Result<Orders> findByOrdersNumber(String ordersNumber){
        return Result.success(ordersService.findByOrdersNumber(ordersNumber));
    }

}
